package com.peam.web.controller.system;

import java.util.List;
import java.util.Set;

import org.activiti.engine.*;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.peam.common.constant.Constants;
import com.peam.common.core.domain.AjaxResult;
import com.peam.common.core.domain.entity.SysMenu;
import com.peam.common.core.domain.entity.SysUser;
import com.peam.common.core.domain.model.LoginBody;
import com.peam.common.utils.SecurityUtils;
import com.peam.framework.web.service.SysLoginService;
import com.peam.framework.web.service.SysPermissionService;
import com.peam.system.service.ISysMenuService;

/**
 * 登录验证
 * 
 * @author peam
 */
@RestController
public class SysLoginController
{
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;
    @Autowired
    private ProcessEngineConfiguration processEngineConfiguration;
    /**
     * 登录方法
     * 
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody)
    {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }
    @PostMapping("/ceshi")
    public AjaxResult ceshi()
    {
        AjaxResult ajax = AjaxResult.success();

        // 1.创建processEngine
        ProcessEngine processEngine = processEngineConfiguration.buildProcessEngine();
        // 2.获取RepositoryService
        RepositoryService repositoryService = processEngine.getRepositoryService();
        // 2. 获取RuntimeService
        RuntimeService runtimeService = processEngine.getRuntimeService();
        // 2.获取taskService
        TaskService taskService = processEngine.getTaskService();
/*  // 部署流程图

        // 3.使用service进行流程部署 ，定义流程名，把 bpmn 和 png 部署到数据库
        Deployment deployment = repositoryService.createDeployment()
                .name("出差申请流程2")
                .addClasspathResource("bpmn/evection.bpmn20.xml")
                .addClasspathResource("bpmn/evection.png")
                .deploy();
        System.out.println("流程部署id = " + deployment.getId());
        System.out.println("流程部署name = " + deployment.getName());*/
 //启动流程实例

        // 3. 根据流程定义的id启动流程
        ProcessInstance myEvection = runtimeService.startProcessInstanceByKey("acitbiti.bpmn20");

        System.out.println("流程定义id = " + myEvection.getProcessDefinitionId());
        System.out.println("流程实例id = " + myEvection.getId());
        System.out.println("当前活动id = " + myEvection.getActivityId());




/*        // 查询个人待办任务
        // 3.根据流程key 和 任务负责人 查询任务
        List<Task> list = taskService.createTaskQuery()
                .processDefinitionKey("acitbiti.bpmn20")
                .taskAssignee("zhangsan")
                .list();
        for (Task task : list) {
            System.out.println("流程实例id = " + task.getProcessInstanceId());
            System.out.println("任务id = " + task.getId());
            System.out.println("任务负责人id = " + task.getAssignee());
            System.out.println("任务名称 = " + task.getName());
        }*/


/*     完成zhangsan任务
   Task task = taskService.createTaskQuery() //完成zhangsan任务
                .processDefinitionKey("acitbiti.bpmn20")
                .taskAssignee("zhangsan")
                .singleResult();
        System.out.println("流程实例id = " + task.getProcessInstanceId());
        System.out.println("任务id = " + task.getId());
        System.out.println("任务负责人id = " + task.getAssignee());
        System.out.println("任务名称 = " + task.getName());

        taskService.complete(task.getId());*/

            // 获取当前活动的ID
        ProcessInstance currentTaskId = runtimeService.createProcessInstanceQuery()
                .processInstanceId("63f8dfee-2c34-11ee-bae7-a85e4536a4f5")
                .singleResult();
        System.out.println("任务id = " + currentTaskId.getActivityId());



        return ajax;
    }
    /**
     * 获取用户信息
     * 
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public AjaxResult getInfo()
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }

    /**
     * 获取路由信息
     * 
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public AjaxResult getRouters()
    {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
        return AjaxResult.success(menuService.buildMenus(menus));
    }
}
